'use strict';
angular.module('app.auth').controller('LoginCtrl', function ($scope, $location,$cookieStore, $http) {
    //获取当前时间
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                + " " + date.getHours() + seperator2 + date.getMinutes()
                + seperator2 + date.getSeconds();
        return currentdate;
    }
    $scope.credentials = {
        id: '',
        pass: ''
    };
    $scope.warning = '';
    var flag = true;
    $scope.loginPost = function () {
        if ($scope.credentials.id == '' || $scope.credentials.pass == '') {
            alert("账号密码不能为空");
            return;
        }
        if (flag == false) {
            return;
        }
        flag = false;
    	$http.post(baseurl+'/userlogin',$scope.credentials).then(function(res){          
             flag = true;
    		if (res.data.ret == 1) {
                $cookieStore.put('userid',$scope.credentials.id);
                $cookieStore.put('userinfo',res.data.info);
                //保存登陆日志
                var data = {name:'success', info_name:$scope.credentials.id, info_func:'登陆', time: getNowFormatDate(), ip:returnCitySN["cip"]+','+returnCitySN["cname"]};
                 // console.log(data)
                // console.log(baseurl);
                window.location.href = "#/maps";//跳转到实时数据页面
                $http.post(baseurl+"/data/logfile",data).then(function(res){
                    // console.log(res.data);
                })
    		}else{
    			$scope.warning = '用戶名或密码错误';
    		}
    	})
    }
    var code = $location.search().code;

    if (code != null) {
        // alert(code);
        $http.get('http://sxzd365.com/wx_login2?code='+code,{withCredentials : false}).then(function(res){
            // console.log(res.data.ret);
            
            if (res.data.ret == 1) {
                // alert(res.data.info.openid);
                var obj = {info:res.data.info,fid:res.data.fid};
                // alert(baseurl+'/wx_login2');
                $http.post(baseurl+'/wx_login2',obj).then(function(res2){
                    if (res2.data.ret == 1) {
                        $cookieStore.put('userid',res2.data.info.id);
                        $cookieStore.put('userinfo',res2.data.info);
                        //保存登陆日志
                        var data = {name:'success', info_name:res2.data.info.id, info_func:'登陆', time: getNowFormatDate(), ip:returnCitySN["cip"]+','+returnCitySN["cname"]};
                        // console.log(data)
                        $http.post(baseurl+"/data/logfile",data).then(function(res){
                            // console.log(res.data);
                        })
                        // console.log(baseurl);
                        window.location.href = "#/data";//跳转到实时数据页面
                    }else if(res2.data.ret == -1){
                        $scope.warning = '拒绝授权';
                    }else if(res2.data.ret == -2){
                        $scope.warning = '没有父账号';
                    }
                })
            }
        })
    }
    
    // 公众号登录
    $scope.wx_login1 = function(){
        var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe9b3a49046f23992&redirect_uri=http%3A%2F%2Fwww.sxzd365.com%2Fwx_login2&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
        // $http.post('http://118.190.203.202:9069/userlogin',{id:'11',pass:'11'}).then(function(res){
        //     console.log(res.data);
        // })        
        // $http({
        //     method: 'JSONP',
        //     url: 'http://sxzd365.com/wx_login2'
        // }).success(function (msg) {
        //     console.log(msg);
        // });
    }
    // 
    $("#wx_container").css({display:'none'});
    $scope.zhanghao_login = function(){
        $("#zhanghao").css({display:'block'});
        $("#wx_container").css({display:'none'});
    }
    $scope.wx_login = function(){
        // $("#wx_container").css({display:'block'});
        // $("#zhanghao").css({display:'none'});
    }
    // var obj = new WxLogin({
    //                           id:"wx_container", 
    //                           appid: "wxe9898479b06c7518", 
    //                           scope: "snsapi_login", 
    //                           redirect_uri: encodeURIComponent("http://sxzd365.com/wx_login"),
    //                           state: "state",
    //                           style: "",
    //                           href: ""
    //                         });
})